<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">有{{ total }}条考勤审批尚未处理</span>
        <template slot="after">
          <el-button size="small" type="primary" @click="$router.push('/import')">导入</el-button>
          <el-button size="small" type="primary">提醒</el-button>
          <el-button size="small" type="primary">设置</el-button>
          <el-button size="small" type="primary">历史归档</el-button>
          <el-button size="small" type="primary">3月份报表</el-button>
        </template>
      </page-tools>
      <FilterComponent :big-list="bigList" @filterChange="filterChange" />
      <el-card>
        <el-table :data="pageList" border>
          <el-table-column label="序号" type="index">
            <template #default="obj">
              {{ ( pageSetting.page - 1) * pageSetting.size + (obj.$index + 1) }}
            </template>
          </el-table-column>
          <el-table-column width="80" label="姓名" prop="username" />
          <el-table-column width="80" label="工号" prop="workNumber" />
          <el-table-column width="120" label="部门" prop="departmentName" />
          <el-table-column width="120" label="手机" prop="mobile" />
          <el-table-column
            v-for="(day,index) in dayList"
            :key="day"
            width="100"
            :label="day"
          >
            <template #default="{row}">
              {{ row.attendanceRecord[index].adtStatu | formatStatu }}
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          align="middle"
          background
          :total="filterList.length"
          layout="prev, pager, next"
          :page-size="pageSetting.size"
          @current-change="currentChange"
        />
      </el-card>
    </div>
  </div>
</template>

<script>
import FilterComponent from './components/FilterComponent'
import { getAttendancesListMock } from '@/api/attendances'
import AttendanceEnum from '@/api/constant/attendance'
export default {
  components: { FilterComponent },
  filters: {
    formatStatu(id) {
      if (id === 0) {
        return ''
      }
      const obj = AttendanceEnum.holidayType.find(item => Number(item.id) === Number(id))
      return obj ? obj.value : '未知'
    }
  },
  data() {
    return {
      bigList: [],
      total: 0,
      filterList: [], // 过滤后的结果
      pageSetting: {
        page: 1,
        size: 10
      }
    }
  },
  computed: {
    dayList() {
      if (!this.bigList[0]) return []
      return this.bigList[0].attendanceRecord.map(item => item.day.toString())
    },
    pageList() {
      // 这里需要计算出每页的开始和结束下标,截取数组即可
      const endIndex = this.pageSetting.page * this.pageSetting.size
      const beginIndex = endIndex - this.pageSetting.size
      return this.filterList.slice(beginIndex, endIndex)
    }
  },
  async created() {
    this.bigList = await getAttendancesListMock()
    this.filterList = [...this.bigList]
    this.total = this.bigList.length
    // console.log(this.bigList)
  },
  methods: {
    filterChange(newList) {
      this.filterList = newList
    },
    currentChange(newPage) {
      this.pageSetting.page = newPage
    }

  }

}
</script>

<style>

</style>
